package com.aube.mis.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aube.apiv2.vo.push.msg.resp.PushPageRelateDataVo;
import com.aube.mis.dto.PaginationResultDTO;
import com.aube.mis.dto.PushDTO;
import com.aube.mis.dto.PushRelatedTagDTO;
import com.aube.mis.dto.ShowModelDTO;
import com.aube.mis.service.PushService;
import com.aube.mis.service.ShowSettingService;
import com.aube.mis.vo.DataTableCriterias;

@Controller
@RequestMapping(value = "/push")
public class PushController extends AbstractController {

	private static Logger log = LoggerFactory.getLogger(PushController.class);

	@Resource
	PushService pushService;
	
	@Resource
	ShowSettingService showSettingService;

	@RequestMapping(value = "/list")
	public String pushList(HttpServletResponse response, ModelMap map) {
		List<ShowModelDTO> modelList = showSettingService.getModelList(getShowId());
		map.put("modelList", modelList);
		return "/push/list";
	}

	@RequestMapping(value = "/list/data.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String pushData(@ModelAttribute DataTableCriterias criterias) {
		PaginationResultDTO<PushDTO> result = pushService.getPushList(criterias, getShowId());
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("list", result.getResultList());
		dataMap.put("total", result.getTotal());
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String pushEdit(HttpServletResponse response, ModelMap map, String pushId) throws Throwable {
		if(StringUtils.isNotEmpty(pushId)) {
			PushDTO pushDetail = pushService.getPushDetail(pushId);
			map.put("push", pushDetail);
			map.put("isEdit", true);
		} else {
			map.put("isEdit", false);
		}
		List<PushRelatedTagDTO> relatedTagList = pushService.getPushRelatedTagList();
		map.put("relatedTagList", relatedTagList);
		return "/push/edit.page";
	}

	@RequestMapping(value = "/edit.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String pushEditAjax(@ModelAttribute PushDTO pushDTO) throws Throwable {
		pushService.editPush(pushDTO);
		return this.createSuccessResponse();
	}

	@RequestMapping(value = "/getRelatedData.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String getRelatedData(String relatedTag) throws Throwable {
		PushPageRelateDataVo relatedData = pushService.getPushRelateData(getShowId(), relatedTag);
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("relatedData", relatedData);
		return this.createSuccessResponse(dataMap);
	}

	@RequestMapping(value = "/delete.ajax", produces = "text/plain;charset=UTF-8")
	@ResponseBody
	public String pushDelete(String pushId) throws Throwable {
		pushService.deletePush(pushId);
		return this.createSuccessResponse();
	}
}
